/**
 * IndexCtrl.java   2017年4月14日 下午4:46:27 by xuebing 
 *
 * Copyright (c) 2010 - 2017 All rights reserved.
 * 
 */
package com.rongmai.web.ctrl;

import com.rongmai.db.model.BaseResult;
import com.rongmai.db.model.UserImg;
import com.rongmai.db.utils.FileUtils;
import com.rongmai.web.bo.FileBo;
import com.rongmai.web.bo.UserBo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/file")
public class FileCtrl {

    @Resource
    private UserBo userBo;
    @Resource
    private FileBo fileBo;

    @RequestMapping(value="/upload1", method = RequestMethod.POST)
    public BaseResult<UserImg> upload(HttpServletRequest request){
        StandardMultipartHttpServletRequest stRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap<String, MultipartFile> multiFileMap = stRequest.getMultiFileMap();
        MultipartFile file = multiFileMap.getFirst("file");
        try {
            return BaseResult.succ(fileBo.saveFile(file));
        } catch (Exception e) {
            log.error("更新用户异常", e);
            return BaseResult.FAIL;
        }
    }

    @RequestMapping(value="/upload", method = RequestMethod.POST)
    public BaseResult<UserImg> upload(MultipartFile file){
        try {
            return BaseResult.succ(fileBo.saveFile(file));
        } catch (Exception e) {
            log.error("更新用户异常", e);
            return BaseResult.FAIL;
        }
    }

    @RequestMapping(value="/download", method = RequestMethod.GET)
    public BaseResult<byte[]> download(String path){
        try {
            return BaseResult.succ(FileUtils.getImageFileInputStream(path));
        } catch (Exception e) {
            log.error("更新用户异常", e);
            return BaseResult.FAIL;
        }
    }



}
